package com.kpx.app.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.adapter.UploadAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.UploadBean;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.DialogFactoryImpl;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/5.
 * 上传管理页面
 */

public class ActivityUploadManager extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    ArrayList<UploadBean> uploadBeens = new ArrayList<>();
    private UploadAdapter adapter;
    private boolean isGetData = false;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_upload_manager);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "上传管理");
        outView.setPullLoadEnable(false);
        outView.setPinnedTime(1000);
        outView.setAutoLoadMore(false);
//		outView.setCustomHeaderView(new CustomHeader(this));
//		outView.setCustomHeaderView(new XRefreshViewHeader(this));
        outView.setMoveForHorizontal(true);
//        outView.setCustomFooterView(new CustomerFooter(this));
//		outView.setPinnedContent(true);
    }

    @Override
    public void initData() {
        adapter = new UploadAdapter(this, uploadBeens, R.layout.item_upload_show);
        lvShow.setAdapter(adapter);
        lvShow.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                DialogFactoryImpl.newInstance(ActivityUploadManager.this).showConfirmDialog("确定", "取消", "是否删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        switch (which) {
                            case DialogInterface.BUTTON_NEGATIVE:
                                deleteTask(uploadBeens.get(position));
                                break;
                            case DialogInterface.BUTTON_POSITIVE:

                                break;

                        }
                    }
                });
                return false;
            }
        });
        getData();
//        for (int i = 0; i < uploadBeens.size(); i++) {
//            if (uploadBeens.get(i).getStatus() == UploadBean.UPLOAD_TYPE_WAITTING) {
//                Intent intent = new Intent();
//                intent.setAction(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_BEGIN);
//                intent.putExtra(Constants.KEY_UPLOAD_BEAN, uploadBeens.get(i));
//                sendBroadcast(intent);
//                break;
//            }
//        }
        outView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
//                getData();
                outView.stopRefresh();
            }

            @Override
            public void onLoadMore(boolean isSlience) {

            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double offset, int offsetY) {

            }
        });
    }

    public void deleteTask(UploadBean position) {
        uploadBeens.remove(position);
        FileCache.sharedInstance().put(Constants.KEY_UPLOADS_LIST, uploadBeens);
        adapter.setData(uploadBeens);
        if (uploadBeens.size() == 0) {
            txtEmptyMention.setText("您还没有相关上传任务哦");
            txtEmptyMention.setVisibility(View.VISIBLE);
            if (getIntent().getExtras() != null && (getIntent().getExtras().getInt("isPhoto") == 1 || getIntent().getExtras().getInt("isUpPic") == 1)) {
                finish();
                ToastUtil.getInstance().showToast(this, "上传完成");
            }
        } else {
            txtEmptyMention.setVisibility(View.GONE);
        }
    }

    private void getData() {
        if (isGetData) {
            return;
        }
        isGetData = true;
        uploadBeens.clear();
        ArrayList<UploadBean> temuploadBeens = (ArrayList<UploadBean>) FileCache.sharedInstance().get(Constants.KEY_UPLOADS_LIST);
        if (temuploadBeens != null) {
            for (int i = 0; i < temuploadBeens.size(); i++) {
                if (temuploadBeens.get(i).getStatus() != UploadBean.UPLOAD_TYPE_BIND_SUCCESS) {
                    uploadBeens.add(temuploadBeens.get(i));
                }
            }
        }
        FileCache.sharedInstance().put(Constants.KEY_UPLOADS_LIST, uploadBeens);
        adapter.setData(uploadBeens);
        if (uploadBeens.size() == 0) {
            txtEmptyMention.setText("您还没有相关上传任务哦");
            txtEmptyMention.setVisibility(View.VISIBLE);
        } else {
            txtEmptyMention.setVisibility(View.GONE);
        }
        outView.stopRefresh();
        isGetData = false;
    }

    @Override
    public void onReceiver(Context context, Intent intent) {
        if (intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_PROGRESS)) {
            UploadBean b = (UploadBean) intent.getSerializableExtra(Constants.KEY_UPLOAD_BEAN);
            if (b != null) {
                refreshData(b);
                if (txtEmptyMention.getVisibility() == View.VISIBLE) {
                    txtEmptyMention.setVisibility(View.GONE);
                }
            }
        }
    }

    /**
     * 刷新上传UI
     *
     * @param b
     */
    private void refreshData(UploadBean b) {
        if (uploadBeens.size() == 0) {
            getData();
        }
        for (int i = 0; i < uploadBeens.size(); i++) {
            if (uploadBeens.get(i).getImageID() == b.getImageID()) {
                if (uploadBeens.get(i).getStatus() == UploadBean.UPLOAD_TYPE_BIND_SUCCESS) {
                    deleteTask(uploadBeens.get(i));
                    return;
                }
                uploadBeens.get(i).setFailMsg(b.getFailMsg());
                uploadBeens.get(i).setStatus(b.getStatus());
                uploadBeens.get(i).setUploaProgress(b.getUploaProgress());
                break;
            }
        }
        adapter.setData(uploadBeens);
    }

}
